package com.ruoyi.api.service;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import com.ruoyi.standingbook.evaluate.domain.Evaluate;
import com.ruoyi.standingbook.evaluate.mapper.EvaluateMapper;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.api.dto.StatDateEnumDto;
import com.ruoyi.api.dto.evaluate.EnvironmentTypeScoreDto;
import com.ruoyi.api.dto.evaluate.EnvironmentYearStatDto;
import com.ruoyi.api.dto.evaluate.resp.EnvironmentMonthStatisticsRespDto;
import com.ruoyi.api.dto.evaluate.resp.EnvironmentYearStatisticsRespDto;
import com.ruoyi.common.tools.enums.StatDateEnum;
import com.ruoyi.common.tools.enums.StatTimeEnum;
import com.ruoyi.standingbook.evaluate.service.IEvaluateService;
import com.ruoyi.stat.evaluate.domain.EvaluateStat;
import com.ruoyi.stat.evaluate.mapper.EvaluateStatMapper;
import com.ruoyi.stat.evaluate.service.IEvaluateStatService;

import cn.hutool.core.util.StrUtil;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor
@Service
public class EvaluateStatService {

	private final IEvaluateStatService iEvaluateStatService;
	private final EvaluateStatMapper baseMapper;
	private final IEvaluateService evaluateService;
    private final EvaluateMapper evaluateMapper;

	public JSONObject getEnvironmentMonthStatistics() {
		//EnvironmentMonthStatisticsRespDto resDto = new EnvironmentMonthStatisticsRespDto();
		//resDto.setCurrentScore(this.getAvgScore(StatDateEnum.currMonth));
		//resDto.setPreviousScore(this.getAvgScore(StatDateEnum.prevMonth));
        JSONObject obj = new JSONObject();
        Evaluate evaluate = new Evaluate();
        evaluate.setItemName("玉塘街道项目");
        evaluate.setPublishDate(new Date());
        String curScore = evaluateService.getAvgScoreMonth(evaluate);
        //resDto.setCurrentScore(curScore);
        obj.set("currentScore",curScore);

        //resDto.setCurrent(evaluateMapper.getAvgScoreMonth(evaluate));
        obj.set("current",evaluateMapper.getAvgScoreMonth(evaluate));

        evaluate.setPublishDate(com.ruoyi.common.utils.DateUtils.addMonth(new Date(),-1));
        String prvScore = evaluateService.getAvgScoreMonth(evaluate);
        //resDto.setPreviousScore(prvScore);
        obj.set("previousScore",prvScore);

		//resDto.setPrevious(this.getEnvironmentTypeScoreDtoList(StatDateEnum.prevMonth));
        obj.set("previous",evaluateMapper.getAvgScoreMonth(evaluate));

		//resDto.setDeductMarksList(this.evaluateService.getEnvironmentDeductMarksDtoList(com.ruoyi.common.utils.DateUtils.getFirstDayOfMonth(com.ruoyi.common.utils.DateUtils.datePath())));
        obj.set("deductMarksList",this.evaluateService.getEnvironmentDeductMarksDtoList(com.ruoyi.common.utils.DateUtils.getFirstDayOfMonth(com.ruoyi.common.utils.DateUtils.datePath())));
		//return resDto;
        return obj;
	}

    /**
     * 扣分项
     * @return 大屏 top5 市环境测评数据
     */
//	public EnvironmentYearStatisticsRespDto getEnvironmentYearStatistics() {
//		EnvironmentYearStatisticsRespDto resDto = new EnvironmentYearStatisticsRespDto();
//
//		resDto.setCurrentScore(this.getAvgScore(StatDateEnum.currYear));
//		resDto.setPreviousScore(this.getAvgScore(StatDateEnum.prevYear));
//
//		resDto.setCurrentMonthScore(this.getAvgScore(StatDateEnum.currMonth));
//
//		String queryTime = com.ruoyi.common.tools.utils.DateUtils.getPreDate(StatTimeEnum.year.getDay());
//		resDto.setList(this.getEnvironmentYearStatDtoList(queryTime));
//
//		resDto.setDeductMarksList(this.evaluateService.getEnvironmentDeductMarksDtoList(queryTime));
//
//		return resDto;
//	}

    public JSONObject getEnvironmentYearStatistics() {
        JSONObject obj = new JSONObject();
        Evaluate evaluate = new Evaluate();
        evaluate.setItemName("玉塘街道项目");
        evaluate.setPublishDate(new Date());
        String curScore = evaluateService.getAvgScoreYear(evaluate);
        obj.set("currentScore",curScore);
        Date d = new Date();
        int year = d.getYear();
        int month = d.getMonth()+1;
        JSONArray curArr = new JSONArray();
        for(int i=1;i<=month;i++){
            JSONObject item = new JSONObject();
            item.set("month",i);
            evaluate.setPublishDate(com.ruoyi.common.utils.DateUtils.parseDate(year+"-"+String.format("%02d",i)+"-01"));
            item.set("score",evaluateService.getAvgScoreMonth(evaluate));
            curArr.add(item);
        }
        obj.set("current",curArr);

        evaluate.setPublishDate(com.ruoyi.common.utils.DateUtils.addYears(new Date(),-1));
        String prvScore = evaluateService.getAvgScoreYear(evaluate);
        obj.set("previousScore",prvScore);

        String queryTime = com.ruoyi.common.tools.utils.DateUtils.getPreDate(StatTimeEnum.year.getDay());
        obj.set("deductMarksList",this.evaluateService.getEnvironmentDeductMarksDtoList(queryTime));

        return obj;
    }

	////////////

	public String getAvgScore(StatDateEnum dateEnum) {
		StatDateEnumDto dateEnumDto = StatDateEnum.getStatDateEnumDto(dateEnum);
        System.out.println("============"+dateEnumDto.getYear()+"-"+dateEnumDto.getMonth()+"-"+dateEnumDto.getDay());
		LambdaQueryWrapper<EvaluateStat> lqw = Wrappers.lambdaQuery();
		lqw.eq(EvaluateStat::getYear, dateEnumDto.getYear());
		lqw.eq(!StrUtil.isEmpty(dateEnumDto.getMonth()), EvaluateStat::getMonth, dateEnumDto.getMonth());
		lqw.eq(!StrUtil.isEmpty(dateEnumDto.getDay()), EvaluateStat::getDay, dateEnumDto.getDay());
        lqw.groupBy(EvaluateStat::getServiceTypeName);
        String v = this.baseMapper.getAvgScore(lqw);
		return v == null ? "0" : v;
	}

	public List<EnvironmentTypeScoreDto> getEnvironmentTypeScoreDtoList(StatDateEnum dateEnum) {
		StatDateEnumDto dateEnumDto = StatDateEnum.getStatDateEnumDto(dateEnum);

		LambdaQueryWrapper<EvaluateStat> lqw = Wrappers.lambdaQuery();
		lqw.eq(EvaluateStat::getYear, dateEnumDto.getYear());
		lqw.eq(!StrUtil.isEmpty(dateEnumDto.getMonth()), EvaluateStat::getMonth, dateEnumDto.getMonth());
		lqw.eq(!StrUtil.isEmpty(dateEnumDto.getDay()), EvaluateStat::getDay, dateEnumDto.getDay());
		lqw.groupBy(EvaluateStat::getServiceType);
		return this.baseMapper.getEnvironmentTypeScoreDtoList(lqw);
	}

	public List<EnvironmentYearStatDto> getEnvironmentYearStatDtoList(String date) {
		LambdaQueryWrapper<EvaluateStat> lqw = Wrappers.lambdaQuery();
		lqw.ge(!StrUtil.isEmpty(date), EvaluateStat::getQueryTime, date);
		lqw.groupBy(Arrays.asList(EvaluateStat::getYear, EvaluateStat::getMonth));
		return this.baseMapper.getEnvironmentYearStatDtoList(lqw);
	}
}
